package org.hlopik.riverGuide.client.model;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import java.util.List;
import org.hlopik.riverGuide.client.ApplicationFacade;
import org.hlopik.riverGuide.client.model.dto.RiverGuideDTO;
import org.hlopik.riverGuide.client.services.RiverGuideService;
import org.hlopik.riverGuide.client.services.RiverGuideServiceAsync;
import org.puremvc.java.multicore.patterns.proxy.Proxy;

/**
 *
 * @author f
 */
public class RiverGuideProxy extends Proxy {

    public static final String NAME = "RiverGuideProxy";
    RiverGuideServiceAsync service;

    public RiverGuideProxy() {
        super(NAME, null);
        service = (RiverGuideServiceAsync) GWT.create(RiverGuideService.class);
    }

    public void addRiverGuide(RiverGuideDTO riverGuideDTO) {
        AsyncCallback<Void> callback = new AsyncCallback<Void>() {

            public void onFailure(Throwable caught) {
                onAddRiverGuideFault(caught);
            }

            public void onSuccess(Void result) {
                onAddRiverGuideSuccess();
            }
        };
        service.addRiverGuide(riverGuideDTO, callback);
    }

    public void getRiverGuides() {
        AsyncCallback<List<RiverGuideDTO>> callback = new AsyncCallback<List<RiverGuideDTO>>() {

            public void onFailure(Throwable caught) {
                ongetRiverGuidesFault(caught);
            }

            public void onSuccess(List<RiverGuideDTO> result) {
                onGetRiverGuidesSuccess(result);
            }
        };
        service.getAllRiverGuides(callback);
    }

    private void onAddRiverGuideFault(Throwable caught) {
        sendNotification(ApplicationFacade.ADD_RIVER_GUIDE_FAULT, caught);
    }

    private void onAddRiverGuideSuccess() {
        sendNotification(ApplicationFacade.ADD_RIVER_GUIDE_SUCCESS);
    }

    private void ongetRiverGuidesFault(Throwable caught) {
        sendNotification(ApplicationFacade.GET_RIVER_GUIDES_FAULT, caught);
    }

    private void onGetRiverGuidesSuccess(List<RiverGuideDTO> result) {
        sendNotification(ApplicationFacade.GET_RIVER_GUIDES_SUCCESS, result);
    }
}
